<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/16
 * Time: 22:01
 */

defined('BASEPATH') OR exit('No direct script access allowed');

class Order extends Admin_Controller
{
    public function index()
    {
        
        $this->load->model('order_model');
        $this->load->library('pagination');
        $this->load->model('siteweb_model');
        $this->config->load('data_field');

        $order = $this->config->item('order');
        $assoc = $this->uri->uri_to_assoc();


        $_param_option = array(
            'goods_name' => array(
                'col' => 'col-lg-3',
                'type' => 'text-c',
                'placeholder' => '输入订单号、email、姓名',
                'item' => array(
                    'name' => 'web_id',
                    'placeholder' => '选择站点',
                    'option' => $this->siteweb_model->siteweb_option()
                )
            ),
            'pay_time' => array(
                'col' => 'col-lg-4',
                'type' => 'dpd_time',
                'placeholder' => '付款时间'
            ),
            'order_amount' => array(
                'col' => 'col-lg-2',
                'type' => 'dpd',
                'placeholder' => '总金额'
            ),
            'order_status' => array(
                'col' => 'col-lg-2',
                'type' => 'option',
                'placeholder' => '选择订单状态',
                'option' => $order['order_status']
            ),
            'shipping_status' => array(
        'col' => 'col-lg-2',
        'type' => 'option',
        'placeholder' => '选择发货状态',
        'option' => $order['shipping_status']
    )
        );
        
        $_param = array(
            'order_status' => $this->input->get('order_status') !== null ? intval($this->input->get('order_status')) : -1,
            'shipping_status' => $this->input->get('shipping_status') !== null ? intval($this->input->get('shipping_status')) : -1,
            'order_amount_start' => trim($this->input->get('order_amount_start')),
            'order_amount_end' => trim($this->input->get('order_amount_end')),
            'pay_time_start' => trim($this->input->get('pay_time_start')),
            'pay_time_end' => trim($this->input->get('pay_time_end')),
            'web_id' => intval($this->input->get('web_id')),
            'goods_name' => htmlspecialchars(trim($this->input->get('goods_name'))),
            'page' => $assoc['page'] > 0 ? intval($assoc['page']) - 1 : 0
        );
    
        $_action_link = array(
            array('href' => base_url('delivery/order_ex') . '?' . $_SERVER["QUERY_STRING"], 'name' => '导出订单')
        );

        $config['base_url'] = base_url('order/index/') . 'page/';
        $config['total_rows'] = $this->order_model->order_num($_param);
        $config['num_links'] = 6;
        $config['per_page'] = $this->order_model->limit_rows;
        $config['use_page_numbers']=true;

        $current_page = intval($assoc['page']);

        $this->pagination->initialize($config);

        $data = array(
            'order_list' => $this->order_model->order_list($_param),
            'pages' => $this->pagination->create_links(),
            'site_option' => $this->siteweb_model->siteweb_option(),
            'param_option' => $_param_option,
            'order_s' => $order,
            'param' => $_param,
            'action_link' => $_action_link
        );

        $this->load->view('order_list', $data);
    }

    public function info()
    {

        $this->load->model('order_model');
        $this->load->model('order_goods_model');
        $this->load->model('siteweb_model');

        $this->config->load('data_field');

        $assoc = $this->uri->uri_to_assoc();

        $order_info = $this->order_model->order_info($assoc['order_id']);
        $status = $this->config->item('order');
        $goods_status = $this->config->item('order_goods');
        $order_info['order_status_name'] = $status['order_status'][$order_info['order_status']];
        $order_info['shipping_status_name'] = $status['shipping_status'][$order_info['shipping_status']];
        $order_info['pay_status_name'] = $status['pay_status'][$order_info['pay_status']];
    
        if ($_GET['print'] == 1) {
            $order_info['print_shipping'] = base_url('shopifydata/') . $order_info['order_sn'] . '-' . $order_info['shipping_no'] . '.pdf';
            unset($_GET['print']);
        }
        $order_log = $this->order_model->log_list($order_info['order_id']);

        $data = array(
            'order_info' => $order_info,
            'siteweb_info' => $this->siteweb_model->siteweb_info(array('web_id' => $order_info['web_id'])),
            'order_goods' => $this->order_goods_model->goods_list($assoc['order_id']),
            'status' => $status,
            'goods_status' => $goods_status['status'],
            'order_log' => $order_log
        );

        $this->load->view('order_info', $data);
    }


    public function ajax_add_shipping_no()
    {
        $this->load->model('order_model');

        $this->load->library('sys_message');

        $shipping_no = htmlspecialchars(trim($this->input->post('shipping_no')));
        $order_id = intval($this->input->post('order_id'));

        if ($shipping_no && $order_id) {
            $this->order_model->update_field(array('shipping_no' => $shipping_no, 'order_status' => 3), $order_id);
            $this->sys_message->success('成功!');
        } else {
            $this->sys_message->wrong('请填写运单号!');
        }

        exit;
    }
    
    public function print_info()
    {
    
        $this->load->model('order_model');
        $this->load->model('order_goods_model');
        $this->load->model('siteweb_model');
    
        $this->config->load('data_field');
    
        $assoc = $this->uri->uri_to_assoc();
    
        $order_info = $this->order_model->order_info($assoc['order_id']);
        $status = $this->config->item('order');
        $goods_status = $this->config->item('order_goods');
        $order_info['order_status_name'] = $status['order_status'][$order_info['order_status']];
        $order_info['shipping_status_name'] = $status['shipping_status'][$order_info['shipping_status']];
        $order_info['pay_status_name'] = $status['pay_status'][$order_info['pay_status']];
    
        $order_log = $this->order_model->log_list($order_info['order_id']);
    
        $data = array(
            'order_info' => $order_info,
            'siteweb_info' => $this->siteweb_model->siteweb_info(array('web_id' => $order_info['web_id'])),
            'order_goods' => $this->order_goods_model->goods_list($assoc['order_id']),
            'status' => $status,
            'goods_status' => $goods_status['status'],
            'order_log' => $order_log
        );
    
        $this->load->view('order_print_info', $data);
    }

    public function handle_log()
    {

        $this->load->model('order_model');
        $this->load->model('siteweb_model');

        $this->load->library('sys_message');

        $assoc = $this->uri->uri_to_assoc();

        $order_id = intval($assoc['order_id']);
        $handle_type = intval($this->input->post('handle_type'));
        $log_info = htmlspecialchars(trim($this->input->post('info')));

        $order = $this->order_model->order_info($order_id);

        !$order && $this->sys_message->wrong('订单不存在!');
        
        $siteweb = $this->siteweb_model->siteweb_info(array('web_id' => $order['web_id']));

        $this->load->library('shopify_api', $siteweb);

        $data = array(
            'log_info' => $log_info
        );

        $data_sss = '';
        $ero = 0;
        
        switch ($handle_type) {
            case 1:

                !$order['shipping_no'] && $this->sys_message->wrong('运单号不存在!');

                if ($order['shipping_status'] <= 0) {
                    $this->load->model('goods_sku_model');
                    
 
                    $this->goods_sku_model->subtract_order_goods($order_id);
                    
                    $data['shipping_status'] = 1;
                    $data['log_info'] .= ($data['log_info'] ? '<br /> ' : '') . '运单号上传至Shopify,减去占用库存,设为已发货';
    
                } else {
                    $ero = 1;
                }
    

                $order_shipping = array(
                    'shipping_no' => $order['shipping_no'],
                    'shipping_url' => 'https://www.17track.net/en'
                );
                $fulfillments = $this->shopify_api->order_upload_shipping_no($order['source_id'], $order_shipping);

                $fulfillments['status'] != 'success' && $data_sss = '回传失败!';
                
                break;
            case 2:

                $this->load->model('order_goods_model');
                $this->load->model('goods_sku_model');
                $this->load->model('purchase_goods_model');

                $data['order_status'] = 4;
                $data['log_info'] .= ($data['log_info'] ? '<br /> ' : '') . '取消订单';

                $order_goods = $this->order_goods_model->goods_list($order_id);

                foreach ($order_goods as $row) {

                    if ($row['status'] == 4) {
                        continue;
                    }

                    $this->order_goods_model->release_occupy_inventory_num($row['rec_id'], $row['occupy_inventory_num'], 4);

                    if ($row['occupy_inventory_num'] > 0) {

                        $this->goods_sku_model->take_sku($row['sku_id'], 3, $row['occupy_inventory_num'], -$row['occupy_inventory_num'], "#{$row['order_id']} 订单产品取消,释放占用库存", $row['order_id']);

                        $data['log_info'] .= "<br /> #{$row['rec_id']} 释放 <b>{$row['occupy_inventory_num']}</b> 个占用库存";
                    }

                    $need_num = $row['goods_number'] - $row['occupy_inventory_num'];
                    if ($need_num > 0) {

                        $purchase = $this->purchase_goods_model->subtract_order_purchase_number($row['rec_id'], $need_num);
                        $data['log_info'] .= $purchase ? "<br /> 根据 #{$row['rec_id']} 取消 #{$purchase['pg_id']} 采购产品 <b>{$purchase['number']}</b> 个采购" : '';
                    }
                }

                break;
            case 0:
                !$log_info && $this->sys_message->wrong('请填写备注信息!');
                break;
        }

        if ($ero == 0) {
            $this->order_model->order_log($order_id, $data);
        }
        

        $this->sys_message->success('成功!'.$data_sss);
    }

    public function subtract_order_sku()
    {
        exit;
        $this->load->model('order_model');
        $this->load->model('siteweb_model');

        $this->load->library('sys_message');

        $order_list = $this->order_model->subtract_order_list();

        !$order_list && $this->sys_message->wrong('没有订单!');

        foreach ($order_list as $order) {
            $order['shipping_status'] > 0 && $this->sys_message->wrong('配送状态不是未发货!');

            !$order['shipping_no'] && $this->sys_message->wrong('运单号不存在!');

            $this->load->model('goods_sku_model');

            $this->goods_sku_model->subtract_order_goods($order['order_id']);

            $data['shipping_status'] = 1;
            $data['log_info'] = '减去占用库存,设为已发货';

            $this->order_model->order_log($order['order_id'], $data);
        }
    }

    public function goods_list()
    {
        $this->load->model('order_goods_model');
        $this->load->model('order_model');
        $this->load->library('pagination');
        $this->load->model('siteweb_model');
        $this->config->load('data_field');

        $order_goods = $this->config->item('order_goods');
        $order = $this->config->item('order');
        $assoc = $this->uri->uri_to_assoc();


        $_param_option = array(
            'goods_name' => array(
                'col' => 'col-lg-3',
                'type' => 'text',
                'placeholder' => '输入货号、订单号'
            ),
            'pay_time' => array(
                'col' => 'col-lg-4',
                'type' => 'dpd_time',
                'placeholder' => '付款时间'
            ),
            'status' => array(
                'col' => 'col-lg-2',
                'type' => 'option',
                'placeholder' => '选择状态',
                'option' => $order_goods['status']
            ),
            'order_status' => array(
                'col' => 'col-lg-2',
                'type' => 'option',
                'placeholder' => '选择订单状态',
                'option' => $order['order_status']
            ),
            'shipping_status' => array(
                'col' => 'col-lg-2',
                'type' => 'option',
                'placeholder' => '选择发货状态',
                'option' => $order['shipping_status']
            )
        );

        $_param = array(
            'status' => $this->input->get('status') !== null ? intval($this->input->get('status')) : -1,
            'shipping_status' => $this->input->get('shipping_status') !== null ? intval($this->input->get('shipping_status')) : -1,
            'order_status' => $this->input->get('order_status') !== null ? intval($this->input->get('order_status')) : -1,
            'pay_time_start' => trim($this->input->get('pay_time_start')),
            'pay_time_end' => trim($this->input->get('pay_time_end')),
            'web_id' => intval($this->input->get('web_id')),
            'goods_name' => htmlspecialchars(trim($this->input->get('goods_name'))),
            'page' => $assoc['page'] > 0 ? intval($assoc['page']) - 1 : 0
        );

        $config['base_url'] = base_url('order/goods_list/') . 'page/';
        $config['total_rows'] = $this->order_goods_model->count($_param);
        $config['num_links'] = 6;
        $config['per_page'] = $this->order_model->limit_rows;
        $config['use_page_numbers']=true;

        $this->pagination->initialize($config);

        $goods_list = $this->order_goods_model->get_goods_list($_param);

        $data = array(
            'goods_list' => $goods_list,
            'param_option' => $_param_option,
            'param' => $_param,
            'pages' => $this->pagination->create_links()
        );

        $this->load->view('order_goods_list', $data);
    }

    public function inv_goods_number()
    {
        $this->load->model('order_goods_model');
        $this->load->model('goods_sku_model');
        $this->load->model('purchase_goods_model');
        $this->load->model('order_model');

        $this->load->library('sys_message');

        $assoc = $this->uri->uri_to_assoc();

        $rec_id = intval($assoc['rec_id']);

        $order_goods = $this->order_goods_model->goods_info($rec_id);

        !$order_goods  && $this->sys_message->wrong('订单产品不存在!');

        $order_goods['sku_number'] <= 0  && $this->sys_message->wrong('没有库存!');

        $order_goods['shipping_status'] != 0 && $this->sys_message->wrong('订单已经发货，无需占用!');

        $order_goods['occupy_inventory_num'] >= $order_goods['goods_number'] && $this->sys_message->wrong('已经占用!');

        $data = array();


        $inv_number = $this->order_goods_model->inv_goods_number($rec_id, array(
            'sku_number' => $order_goods['sku_number'],
            'need_number' => $order_goods['goods_number'] - $order_goods['occupy_inventory_num']
        ));
        $_log_info = " #{$rec_id} 订单产品占用 <b>$inv_number</b> 个库存";
        $purchase = $this->purchase_goods_model->subtract_order_purchase_number($rec_id, $inv_number);
        $_log_info .= $purchase ? "<br /> 取消 #{$purchase['pg_id']} 采购产品 <b>{$purchase['number']}</b> 个采购" : '';
        $this->goods_sku_model->take_sku($order_goods['sku_id'], 3, 0, $inv_number, "#{$order_goods['order_id']} 订单占用库存", $order_goods['order_id']);
        $this->order_model->check_orders($order_goods['order_id'], 2, $_log_info);

        $this->sys_message->success('成功!');
    }

    public function cancel_order_goods()
    {
        $this->load->model('order_goods_model');
        $this->load->model('goods_sku_model');
        $this->load->model('purchase_goods_model');
        $this->load->model('order_model');

        $this->load->library('sys_message');

        $assoc = $this->uri->uri_to_assoc();

        $rec_id = intval($assoc['rec_id']);

        $order_goods = $this->order_goods_model->goods_info($rec_id);

        !$order_goods  && $this->sys_message->wrong('订单产品不存在!');

        $order_goods['shipping_status'] != 0 && $this->sys_message->wrong('订单已经发货，无法取消!');

        $data = array();
        $log_info = "#{$rec_id} 取消订单产品";
        $this->order_goods_model->release_occupy_inventory_num($rec_id, $order_goods['occupy_inventory_num'], 4);

        if ($order_goods['occupy_inventory_num'] > 0) {
            $this->goods_sku_model->take_sku($order_goods['sku_id'], 3, $order_goods['occupy_inventory_num'], -$order_goods['occupy_inventory_num'], "#{$order_goods['order_id']} 订单产品取消,释放占用库存", $order_goods['order_id']);

            $log_info .= "<br /> 释放 <b>{$order_goods['occupy_inventory_num']}</b> 个占用库存";
        }

        $need_num = $order_goods['goods_number'] - $order_goods['occupy_inventory_num'];
        if ($need_num > 0) {
            $purchase = $this->purchase_goods_model->subtract_order_purchase_number($rec_id, $need_num);

            $log_info .= $purchase ? "<br /> 取消 #{$purchase['pg_id']} 采购产品 <b>{$purchase['number']}</b> 个采购" : '';
        }
        $this->order_model->check_orders($order_goods['order_id'], 1, $log_info);

        $this->sys_message->success('成功!');
    }
    public function release_goods_number()
    {
        $this->load->model('order_goods_model');
        $this->load->model('goods_sku_model');
        $this->load->model('order_model');
        $this->load->model('purchase_goods_model');
        $this->load->model('purchase_order_model');

        $this->load->library('sys_message');

        $assoc = $this->uri->uri_to_assoc();

        $rec_id = intval($assoc['rec_id']);
		$is_purchase = intval($this->input->post('is_purchase'));

        $order_goods = $this->order_goods_model->goods_info($rec_id);

        !$order_goods  && $this->sys_message->wrong('订单产品不存在!');

        $order_goods['shipping_status'] != 0 && $this->sys_message->wrong('订单已经发货，无法释放库存!');

        $order_goods['occupy_inventory_num'] <= 0 && $this->sys_message->wrong('占用库存为0!');
        $occupy_inventory_num = $this->order_goods_model->release_occupy_inventory_num($rec_id, $order_goods['occupy_inventory_num'],$order_goods['order_id']);


        if ($is_purchase == 1) {
			$log_info = '<br />';
			$order_goods['occupy_inventory_num'] -= $occupy_inventory_num;
			$order_goods['log_info'] = '重新采购';
			$log_info .= $this->purchase_goods_model->purchase_order_goods(array(0 => $order_goods));
			$purchase_goods = $this->purchase_goods_model->unallocated_goods_supp();
			if ($purchase_goods) {
				$this->purchase_order_model->batch_create_order($purchase_goods);
			}
		}
        $data = array();
        if ($order_goods['order_status'] == 2) {
            $data['order_status'] = 1;
        }
        $data['log_info'] = "#{$rec_id} 产品设为重新采购，释放占用库存" . $log_info;
        $this->order_model->order_log($order_goods['order_id'], $data);
        $this->goods_sku_model->take_sku($order_goods['sku_id'], 3, $order_goods['occupy_inventory_num'], -$order_goods['occupy_inventory_num'], "#{$order_goods['order_id']} 订单释放占用库存", $order_goods['order_id']);

        $this->sys_message->success('成功!');
    }

    public function ly_list()
    {
        $this->load->model('lyOrder_model');
        $this->load->library('pagination');

        $assoc = $this->uri->uri_to_assoc();

        $config['base_url'] = base_url('order/ly_list/') . 'page/';
        $config['total_rows'] = $this->lyOrder_model->order_num();
        $config['num_links'] = 6;
        $config['per_page'] = 15;
        $config['use_page_numbers']=true;

        $current_page = intval($assoc['page']);

        if(0 == $current_page)
        {
            $current_page = 1;
        }
        $offset = ($current_page - 1 ) * $config['per_page'];

        $this->pagination->initialize($config);

        $data = array(
            'order_list' => $this->lyOrder_model->order_list($offset,$config['per_page']),
            'pages' => $this->pagination->create_links()
        );

        $this->load->view('order_ly_list',$data);
    }


    /**
     * 新品数据
     * order_five_sum
     * author: walker
     * Date: 2019/12/4
     * Time: 18:31
     * Note:
     */
    public function order_five_sum(){
        $this->load->model('ga_goods_model');
        $assoc = $this->uri->uri_to_assoc();
        $this->load->library('pagination');
        $this->load->model('order_model');

        $_param = array(
            'web_id' => $this->input->get('web_id'),
            'page' => $assoc['page'] > 0 ? intval($assoc['page']) - 1 : 0
        );
        $res = $this->order_model->order_five_sum($_param);
        $config['base_url'] = base_url('order/order_five_sum/') . 'page/';
        $config['total_rows'] = $this->order_model->order_five_sum_page($_param);
        $config['num_links'] = 6;
        $config['per_page'] = $this->order_model->limit_rows;
        $config['use_page_numbers'] = true;
        $this->pagination->initialize($config);
        $data = array(
            'param'=>['web_id'=>$_param['web_id']],
            'siteweb' => $this->ga_goods_model->siteweb_option(),
            'order_list' => $res,
            'pages' => $this->pagination->create_links()
        );
        $this->load->view('order_five_sum',$data);
    }




    public function new_goods(){
        $goods_id = $this->input->post('goods_id');
        $this->load->model('order_model');
        $res = $this->order_model->new_goods($goods_id);
        $this->load->library('sys_message');
        if($res){
            $this->sys_message->success('成功!');
        }else{
            $this->sys_message->wrong('已保存过!');
        }
    }

    public function new_goods_list(){
        $this->load->model('admin_model');
        $this->load->model('ga_goods_model');
        $this->load->model('order_model');
        $this->load->library('pagination');
        $assoc = $this->uri->uri_to_assoc();
        $_param = array(
            'admin_id' => $this->input->get('admin_id'),
            'web_id' => $this->input->get('web_id'),
            'page' => $assoc['page'] > 0 ? intval($assoc['page']) - 1 : 0
        );
        
        $res = $this->order_model->new_goods_list($_param);

        $config['base_url'] = base_url('order/new_goods_list/') . 'page/';
        $config['total_rows'] = $this->order_model->new_goods_list_page($_param);
        $config['num_links'] = 6;
        $config['per_page'] = $this->order_model->limit_rows;
        $config['use_page_numbers'] = true;
        $this->pagination->initialize($config);

        $data = array(
            'admin'=>$this->admin_model->admin_option(),
            'param'=>['web_id'=>$_param['web_id'],'admin_id'=>$_param['admin_id']],
            'siteweb' => $this->ga_goods_model->siteweb_option(),
            'order_list' => $res,
            'pages' => $this->pagination->create_links()
        );
        $this->load->view('new_goods_list',$data);
    }

    public function del_goods(){
        $id = $this->input->post('id');
        $this->load->model('order_model');
        $res = $this->order_model->del_goods($id);
        $this->load->library('sys_message');
        if($res){
            $this->sys_message->success('删除成功!');
        }else{
            $this->sys_message->wrong('不是你的商品!');
        }
    }

    public function k_goods(){
        $this->load->model('order_model');
        $_param = array(
            'dpd1' => $this->input->post('dpd1'),
            'dpd2' => $this->input->post('dpd2'),
            'goods_id' => $this->input->post('goods_id'),
        );
        $res = $this->order_model->k_goods($_param);
    }
}
